/**
 * Created by shidaiapp on 2017/9/29.
 */


/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchService = (self, tiyData) => {
  self.$Spin.show();
  self.$apiPlugin.invalidCustomerInfo({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let userPage = response.result;
      tiyData.tData.userList.length = 0;
      tiyData.tPage.total = userPage.total;
      userPage.list.forEach(
        function (item) {
          tiyData.tData.userList.push(item);
        }
      );
      tiyData.tData.userList.sort(sortKey);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

function sortKey(a,b){
  if(a.userShop != '' && b.userShop == '' ){
      return -1;
  }else if(a.userShop == '' && b.userShop != ''){
    return 1;
  }else {
    return 0;
  }
}

/**
 * 更改用户状态
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const setCustomerEffective = (self, tiyData) => {
  self.$Spin.show();
  self.$apiPlugin.setCustomerEffective({
    "customerId": tiyData.tParams.changeStateCustomerInfo.id,
    "number" : tiyData.tParams.changeStateCustomerInfo.number,
    "currentState" : 300
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      searchService(self, tiyData);
      self.$Message.config("保存状态成功")
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
